<?php
namespace Web\Controller;
use Think\Controller;
use Web\Logic\MenuLogic;
class MenuController extends BaseController {
    public function index(){
        //实例化Logic
        $logic = new MenuLogic();
        //获取父栏目id
        if(!empty($_REQUEST['id']))
        	$wdata['parentid'] = trim($_REQUEST['id']);
        else
        	$wdata = "parentid = 0 or parentid is null";
        //获取父栏目名称
        if(!empty($_REQUEST['fmenu']))
        	$fmenu = "[".trim($_REQUEST['fmenu'])."]子菜单列表";
        else
        	$fmenu = "菜单列表";

        //判断
        //查询总数量
        $count = $logic->selNum($wdata);
        $Page  = $this->getpage($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //产生分页信息
		$show  = $this->bootstrap_page_style($Page->show());
        //查询数据
        $rolerows = $logic->indexList($wdata, $Page);
        $this->assign('rolerows',$rolerows);// 赋值数据集
        $this->assign("fmenu", $fmenu);
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    /**
     * 更新数据
     */
    public function upData(){
    	//接受参数
    	$sortdata = $_REQUEST['sortdata'];
    	//实例化Logic
    	$logic = new MenuLogic();
    	//保存数据
    	$r = $logic->updataSort($sortdata);
    	//判断是否添加成功
    	if($r){
    		$this->success('更新成功！',"index");
    		exit;
    	}
    }

	public function saveSort(){
		$data = I('data');
		$Menu = M("menu");
		foreach($data as $v){
			$Menu->where(array('menuid'=>$v['id']))->setField('sorts', $v['sort']);
		}
		echo '1';
	}

    /**
     * 添加数据
     */
    public function add(){
        //获取父栏目id
        $id =  isset($_REQUEST['id'])? trim($_REQUEST['id']) :0 ;
        //获取父栏目名
        $fmenu =  isset($_REQUEST['fmenu'])? trim($_REQUEST['fmenu']) :"";
        //实例化Logic
        $logic = new MenuLogic();
        //接收提交按钮
        $sub = $_POST['sub'];
        if($sub)
        {
            //接受添加数据
            $data = $_REQUEST['data'];
            //判断 菜单名是否存在
            if($data['menuname'])
            {
                $data['updatetimes'] = date("Y-m-d H:i:s");

                $r = $logic->addDataSql($data);
                //判断是否添加成功
                if($r){
                    $this->success('添加成功！',"index");
                    exit;
                }
            }
        }
        //获取导航栏
        $listdhl =  $logic->getDhl(0);
        $this->assign("listdhl",$listdhl);
        $this->assign("id",$id);
        $this->assign("fmenu",$fmenu);
        $this->display();
    }

    /**
     * @desc 修改科室
     */
    public function edit()
    {
    	//接受修改id
    	$id = $_REQUEST['id'];
    	//接受表单提交按钮
    	$sub = $_REQUEST['sub'];
    	//实例化Logic
    	$logic = new MenuLogic();
    	if($sub)
    	{
    		//接受参数     修改数据
    		$data = $_REQUEST['data'];
    		$data['updatetimes'] = date("Y-m-d H:i:s");

    		$r = $logic->saveDataSql($id, $data);
    		if($r)
    		{
    			$this -> success('保存成功！',"index");
    			exit;
    		}
    	}

    	//查询信息
    	$listRows = $logic->selOneSql($id);
    	//获取父菜单
    	$flm =  $logic->selOneSql($listRows['parentid']);

    	if($flm['menuname'])
    	{
    		$listRows['fmenu'] = $flm['menuname'];
    	}
    	else
    	{
    		$listRows['fmenu'] = "无";
    	}

    	$this->assign("listrows", $listRows);
    	$this->display();

    }

    /**
     * @desc 栏目删除
     * @param id 栏目id
     */
    public function deletes()
    {
    	//接受 menuid
    	$id = $_REQUEST['id'];
    	//实例化Logic
    	$logic = new MenuLogic();
    	$des3 = $logic->delSql($id);
    	if($des3)
    	{
    		$this->success('删除成功', "index");
    		exit;
    	}
    }



    /**
     * @desc 获取子菜单类别
     * @param id 父id
     */
    public function getzl()
    {
    	//实例化Logic
    	$logic = new MenuLogic();
    	//获取父栏目id
    	$parentid = $_REQUEST["parentid"];

    	$listrow = $logic->getDhl($parentid);

    	$count = count($listrow);

    	$rew="";
    	if($count > 0)
    	{
    		$rew = " <select id='mdata' name='mdata' onchange='if(this.value!=0){menuselect(this)}'>
                <option value='0'>无</option>";
    		if(!empty($listrow )){
    			foreach($listrow as $md){
    				$rew .="<option value='$md[menuid]'>$md[menuname]</option>";
    			}
    		}
    		$rew .="</select>";
    	}

    	//返回数据
    	$dreturn['strdiv'] = $rew;
    	$this->ajaxReturn($dreturn);
    }
}